#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct
{
    int grade[5];  //第五个是为了最终的查找方便而设
    int bestrank;  //记录当前最好排名
    int bestindex; //记录最好排名的科目对应下标
} stu_t;
char course[5] = "ACME";//兼顾优先级
int grade;
bool cmp(stu_t l, stu_t r)
{
    return l.grade[grade] > r.grade[grade];
}
int main()
{
    stu_t stu[2000];
    stu_t *stuid[1000001] = {0}; //记录学生信息对应的结构体指针，为查询方便而设
    int N, M;
    scanf("%d%d", &N, &M);
    int i;
    int id;
    for (i = 0; i < N; ++i)
    {
        scanf("%d", &id);
        stuid[id] = stu + i;
        scanf("%d%d%d", stu[i].grade + 1, stu[i].grade + 2, stu[i].grade + 3);
        stu[i].grade[0] = (stu[i].grade[1] + stu[i].grade[2] + stu[i].grade[3]) / 3;
        stu[i].grade[4] = N - i;
    }
    int rank; //记录当前排名
    for (grade = 0; grade < 4; ++grade)
    {
        sort(stu, stu + N, cmp);
        rank = 1;
        if (0 == grade || 1 < stu[0].bestrank)
        {
            stu[0].bestrank = 1;
            stu[0].bestindex = grade;
        }
        for (i = 1; i < N; ++i)
        {
            if (stu[i].grade[grade] != stu[i - 1].grade[grade])
            { //分数不相同，更新排名
                rank = i + 1;
            }
            if (0 == grade || rank < stu[i].bestrank)
            {
                stu[i].bestrank = rank;
                stu[i].bestindex = grade;
            }
        }
    }
    sort(stu, stu + N, cmp);
    while (M--)
    {
        scanf("%d", &id);
        if (stuid[id] != NULL)
        {
            printf("%d %c\n", stuid[id]->bestrank, course[stuid[id]->bestindex]);
        }
        else
        {
            printf("N/A\n");
        }
    }
    return 0;
}